"""
获取所有的歌手信息
"""
import peewee as pw
from bs4 import BeautifulSoup

from awesomest_spider import glb


class Netease(glb.Model):
    id = pw.CharField(primary_key=True)
    name = pw.CharField()


def main():
    params = {'id': 1003, 'initial': -1}
    r = glb.client.send_request('get', url='https://music.163.com/discover/artist/cat', params=params)

    # 网页解析
    soup = BeautifulSoup(r.content.decode(), 'html.parser')
    body = soup.body

    artists = body.find_all('a', attrs={'class': 'nm nm-icn f-thide s-fc0'})

    for artist in artists:
        artist_id = artist['href'].replace('/artist?id=', '').strip()
        artist_name = artist['title'].replace('的音乐', '')
        data = {'id': artist_id, 'name': artist_name}
        Netease.replace(data).execute()
        yield data


model = Netease
if __name__ == '__main__':
    main()
